/*
 * @(#)ExceptionPopulateAction.java
 *
 * Copyright 2012 by ShenY BIS, All rights reserved.
 */

package com.bis.framework.exception.action;

import java.util.List;

import org.apache.log4j.Logger;

import com.bis.framework.base.action.BaseAction;
import com.bis.framework.exception.ValidateException;
import com.bis.framework.exception.entity.ExecuteResult;
import com.opensymphony.xwork2.ActionContext;

/**
 * ExceptionPopulateAction
 * 
 * @author BIS 2012-12-24
 */
public class ExceptionPopulateAction extends BaseAction
{

    /**
     * Version ID
     */
    private static final long serialVersionUID = 1L;

    /**
     * Logger
     */
    private static Logger     logger           = Logger.getLogger(ExceptionPopulateAction.class);

    /**
     * Action Context
     */
    private ActionContext     actionContext    = ActionContext.getContext();

    /**
     * handleValidateException
     * @Description:
     * @return
     */
    public String handleValidateException()
    {
        Throwable exception = retriveException();
        if (null != exception)
        {
            logger.debug("ValidateException......", exception);
            ValidateException validateException = ((ValidateException) exception);
            List<String> lstMessage = validateException.getLstMessage();
            StringBuilder sbMessage = new StringBuilder();
            for (String message : lstMessage)
            {
                sbMessage.append(message + "<br>");
            }
            executeResult.setMessage(sbMessage.toString());
            if (null != exception.getCause())
            {
                executeResult.setCauseMessage(exception.getCause().toString());
            }
            executeResult.setType(ExecuteResult.VALIDATE_ERROR);
            if (null != validateException.getPageComponentId())
            {
                executeResult.setPageComponentId(validateException.getPageComponentId());
            }

        }
        else
        {
            logger.debug("ValidateException......null");
        }
        return SUCCESS;
    }

    /**
     * handleBusinessException
     * @Description:
     * @return
     */
    public String handleBusinessException()
    {
        Throwable exception = retriveException();
        if (null != exception)
        {
            Throwable t = exception.getCause();
            if (null == t)
            {
                t = exception;
            }
            logger.debug("BusinessException......", t);
            executeResult.setMessage(t.getMessage());
        }
        else
        {
            logger.debug("BusinessException......null");
        }
        executeResult.setType(ExecuteResult.BUSINESS_ERROR);
        return SUCCESS;
    }

    /**
     * handleSystemException
     * @Description:
     * @return
     */
    public String handleSystemException()
    {
        Throwable exception = retriveException();
        if (null != exception)
        {
            Throwable t = exception.getCause();
            if (null == t)
            {
                t = exception;
            }
            logger.debug("SystemException......", t);
            executeResult.setMessage(t.getMessage());
        }
        else
        {
            logger.debug("SystemException......null");
        }
        executeResult.setType(ExecuteResult.SYSTEM_ERROR);
        return SUCCESS;
    }

    /**
     * handleRuntimeException
     * @Description:
     * @return
     */
    public String handleRuntimeException()
    {
        Throwable exception = retriveException();
        if (null != exception)
        {
            Throwable t = exception.getCause();
            if (null == t)
            {
                t = exception;
            }
            logger.debug("RuntimeException......", t);
            executeResult.setMessage(t.getMessage());
        }
        else
        {
            logger.debug("RuntimeException......null");
        }
        executeResult.setType(ExecuteResult.RUNTIME_ERROR);
        return SUCCESS;
    }

    /**
     * handleException
     * @Description:
     * @return
     */
    public String handleException()
    {
        Throwable exception = retriveException();
        if (null != exception)
        {
            Throwable t = exception.getCause();
            if (null == t)
            {
                t = exception;
            }
            logger.debug("Exception......", t);
            executeResult.setMessage(t.getMessage());
        }
        else
        {
            logger.debug("Exception......null");
        }
        executeResult.setType(ExecuteResult.UNEXCEPTED_ERROR);
        return SUCCESS;
    }

    /**
     * retriveException
     * @Description:
     * @return
     */
    protected Throwable retriveException()
    {
        Throwable exception = (Throwable) actionContext.getValueStack().findValue("exception");
        return exception;
    }

}
